//
//  VisitorView.swift
//  WeoboWithSteveLi
//
//  Created by SteveLi on 2017/9/19.
//  Copyright © 2017年 李军桦. All rights reserved.
//

import UIKit
import SnapKit

//访客视图 --处理用户未登录的视图显示
class VisitorView: UIView {
    
    //MARK: 设置视图信息
    /// 设置视图信息
    ///
    /// - parameter imageName: 图片名称，首页设置为nil
    /// - parameter title:     消息文字
    func setupInfo(imageName:String?, title: String) {
        messagrLabel.text = title
        //如果图片名称为nil说明是首页,直接返回
        guard let imgName = imageName else {
            //播放动画
            startAnim()
            return
        }
        iconView.image = UIImage.init(named: imgName)
        //隐藏小房子
        homeIconView.isHidden = true
        //将遮罩图像移动到底层
        sendSubview(toBack: maskIconView)
        
    }
    
    //开启首页转轮动画
    func startAnim(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * Double.pi
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        
        //用在不断重复的动画，当动画绑定的图层对应的视图被销毁，动画会自动被销毁
        anim.isRemovedOnCompletion = false
        //添加图层
        iconView.layer.add(anim, forKey: nil)
    
    }
    
    
    //MARK: -构造函数

    
    //initWithFrame 是UIView的指定的构造函数
    //使用纯代码开发
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
        
    }
    //initWithCoder --- 使用sb或者xib开发加载的函数
    //使用sb开发的入口
    required init?(coder aDecoder: NSCoder) {
        //导致如果使用sb开发，调用这个视图，会直接崩溃
        //阻止使用SB使用当前自定义视图
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setupUI()
    }
    //MARK: - 懒加载控件
    /// 图标使用 image：构造函数创建的 imageView默认就是image大小
    lazy var iconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    //遮罩图形
    lazy var maskIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    //小房子
    lazy var homeIconView:UIImageView  = UIImageView(imageName: "visitordiscover_feed_image_house")
    //消息文字
    lazy var messagrLabel: UILabel = UILabel(title: "关注一些人,回这里看看有什么惊喜关注一些人,回这里看看有什么惊喜")
        
    /// 注册按钮 //common_button_white_disable
    lazy var registerButton: UIButton = UIButton(title: "注册", color: UIColor.orange, backImageName: "common_button_white_disable")    //登录按钮
    lazy var loginButton: UIButton = UIButton(title: "登录", color: UIColor.darkGray, backImageName: "common_button_white_disable")
    
    }
extension VisitorView {
    //设置界面
    func setupUI() {
        //1.添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messagrLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //2.设置自动布局
        /**
            添加约束需要添加到父视图上
            建议，子实视图最好有一个统一的参照物
        */
        //iconView.translatesAutoresizingMaskIntoConstraints默认是true,支持使用setFrame的方式设置控件位置，false支持使用自动布局来设置控件位置

//        //1.图标
//        iconView.translatesAutoresizingMaskIntoConstraints = false
//        homeIconView.translatesAutoresizingMaskIntoConstraints = false
//        //1.图标
//        addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
//         addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: -60))
        //make 要添加的约束对象
        iconView.snp.makeConstraints { (make) in
            //指定centerX 属性 等于 ‘参照对象’ , 'snp_'参照属性的值
            //offset 就是指定相对约束的偏移量
            make.centerX.equalTo(self.snp.centerX)
            make.centerY.equalTo(self.snp.centerY).offset(-60)
        }
    
        
//        //2.小房子
//        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .centerX, relatedBy: .equal, toItem: iconView, attribute: .centerX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .centerY, relatedBy: .equal, toItem: iconView, attribute: .centerY, multiplier: 1.0, constant: 0))
        homeIconView.snp.makeConstraints { (make) in
            make.center.equalTo(iconView.snp.center)
        }
        
//        //3.消息文字
//        addConstraint(NSLayoutConstraint(item: messagrLabel, attribute: .centerX, relatedBy: .equal, toItem: iconView, attribute: .centerX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: messagrLabel, attribute: .top, relatedBy: .equal, toItem: iconView, attribute: .bottom, multiplier: 1.0, constant: 16))
//        addConstraint(NSLayoutConstraint(item: messagrLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 224))
//        addConstraint(NSLayoutConstraint(item: messagrLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 36))
        messagrLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconView.snp.centerX)
            make.top.equalTo(iconView.snp.bottom).offset(16)
            make.width.equalTo(224)
            make.height.equalTo(36)
        }
        
//        //4.注册按钮
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .left, relatedBy: .equal, toItem: messagrLabel, attribute: .left, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .top , relatedBy: .equal, toItem: messagrLabel, attribute: .bottom, multiplier: 1.0, constant: 16))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
//          addConstraint(NSLayoutConstraint(item: registerButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 36))
        registerButton.snp.makeConstraints { (make) in
            make.left.equalTo(messagrLabel.snp.left)
            make.top.equalTo(messagrLabel.snp.bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
        
        
//        //5.登录按钮
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .right, relatedBy: .equal, toItem: messagrLabel, attribute: .right, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .top , relatedBy: .equal, toItem: messagrLabel, attribute: .bottom, multiplier: 1.0, constant: 16))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 36))
//        
        loginButton.snp.makeConstraints { (make) in
            make.right.equalTo(messagrLabel.snp.right)
            make.top.equalTo(registerButton.snp.top)
            make.width.equalTo(registerButton.snp.width)
            make.height.equalTo(registerButton.snp.height)
        }
        //6.遮罩图形
        /* VFL：可视化格式语言
         H水平方法
         V垂直方向
         | 边界
         []包装控件
         views:是一个字典 [名字：控件名] -VFL 字符串中表示控件的字符串
         mrtrics :是一个字典[名字：NSNumber] - VFL字符串中表示某一个数值
         */
        
//        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask":maskIconView]))
//        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[mask]-(btnHeight)-[regButton]", options: [], metrics: ["btnHeight":-36], views: ["mask":maskIconView,"regButton":registerButton]))

        
        maskIconView.snp.makeConstraints { (make) in
            make.top.equalTo(self.snp.top)
            make.left.equalTo(self.snp.left)
            make.right.equalTo(self.snp.right)
            make.bottom.equalTo(registerButton.snp.bottom)
        }
        //设置背景颜色  -灰度图 R = G = B,在UI元素中大多数使用灰度图，或者纯色图（安全色）
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        //3.添加监方法
       
    }
}



